app
.controller('formSelfCtrl', ['$scope', '$http', FormSelfController]);
function FormSelfController($scope, $http){
	$scope.changeValueMap = function(field) {
		if (field == 'exchangeRate') {
			// 计算每一条的人民币金额，保留两位小数
			var exchangeRate = parseFloat($scope.valueMap['exchangeRate']); // 计算汇率
			if(exchangeRate >= 0) {
				for (var i = 0; i < $scope.subGrid.sonlist.length; i++) {
					$scope.subGrid.sonlist[i]['amountCny'] = exchangeRate ? Math.round(($scope.subGrid.sonlist[i].amount) * exchangeRate * 100) / 100 : 0;
				}
				
				// 计算付款CNY总金额
				var amountTotalCny = 0;
				for (var i = 0; i < $scope.subGrid.sonlist.length; i++) {
					var amount = parseFloat($scope.subGrid.sonlist[i]['amount']);
					if(amount) {
						amountTotalCny = amountTotalCny + Math.round(amount * exchangeRate * 100) / 100;
					}
				}
				$scope.valueMap['amountTotalCny'] = Math.round(amountTotalCny * 100) / 100.00;
			}
		}
	}
	
	/*
	 * 数字框变动
	 */
	$scope.subGrid.numberChange = function(row, field) {
		var index = row.index;
		if (field == 'amount') {
			var exchangeRate = parseFloat($scope.valueMap['exchangeRate']); // 计算汇率
			
			// 计算付款人民币总金额
			if (exchangeRate >= 0) {
				row.amountCny = exchangeRate ? Math.round((row.amount) * exchangeRate * 100) / 100 : 0;
				
				var amountTotalCny = 0;
				for (var i = 0; i < $scope.subGrid.sonlist.length; i++) {
					var amount = parseFloat($scope.subGrid.sonlist[i]['amount']);
					if(amount) {
						amountTotalCny = amountTotalCny + Math.round(amount * exchangeRate * 100) / 100;
					}
				}
				$scope.valueMap['amountTotalCny'] = Math.round(amountTotalCny * 100) / 100;
			}
			
			// 计算付款外币总金额
			{
				var amountTotalLocal = 0;
				for (var i = 0; i < $scope.subGrid.sonlist.length; i++) {
					var amount = parseFloat($scope.subGrid.sonlist[i]['amount']);
					if(amount) {
						amountTotalLocal = amountTotalLocal + amount;
					}
				}
				$scope.valueMap['amountTotalLocal'] = amountTotalLocal;
			}
		}
	}
}

/*
 * 子列表控制器
 */
app.controller('subGridCtrl', ['$scope', '$http', function SubGridController($scope, $http){
	/*
	 * 复选框回调
	 */
	$scope.checkbox.afterSelectDatas = function(rowsSelected) {
		var payType = $scope.valueMap['payType']; // 付款类型
		var exchangeRate = parseFloat($scope.valueMap['exchangeRate']); // 计算汇率

		for (var i = 0; i < rowsSelected.length; i++) {
			var row = rowsSelected[i];
			var isAdd = true; // 根据现有存在，是否加新行
			for (var j = 0; j < $scope.subGrid.sonlist.length; j++) {
				if ($scope.subGrid.sonlist[j].relativeId == row.id) {
					isAdd = false;
				}
			}
			if (isAdd) {
				var fee = 0;
				var remark = '';
				if (payType == 1) {
					remark = '透明计划账单编码：' + (row.platformBid ? row.platformBid : '');
				} else if (payType == 2) {
					remark = 'VAT号：' + (row.vatNo ? row.vatNo : '');
				}
				
				$scope.subGrid.sonlist.push({
					relativeId: row.id,
					vgsbm: row.vgsbm,
					vgsjc: row.vgsjc,
					vyhzhmc: row.vyhzhmc,
					vyhzh: row.vyhzh,
					vkhh: row.vkhh,
					vaba: row.vaba,
					swiftCode: row.swiftCode,
					amount: row.billingAmount,
					currencyCode: row.currencyCode,
					remark: remark,
					vjylx: payType == 1 ? '运营服务→透明计划标签费用' : (payType == 2 ? '运营服务→VAT税费' : ''),
					fee: fee,
					amountCny: exchangeRate ? Math.round((row.billingAmount + fee) * exchangeRate * 100) / 100 : 0
				});
			}
		}
		// 计算付款CNY总金额
		if(exchangeRate >= 0) {
			var amountTotalCny = 0;
			for (var i = 0; i < $scope.subGrid.sonlist.length; i++) {
				var amount = parseFloat($scope.subGrid.sonlist[i]['amount']);
				if(amount) {
					amountTotalCny = amountTotalCny + Math.round(amount * exchangeRate * 100) / 100;
				}
			}
			$scope.valueMap['amountTotalCny'] = Math.round(amountTotalCny * 100) / 100;
		}
		
		// 计算付款外币总金额
		{
			var amountTotalLocal = 0;
			for (var i = 0; i < $scope.subGrid.sonlist.length; i++) {
				var amount = parseFloat($scope.subGrid.sonlist[i]['amount']);
				if(amount) {
					amountTotalLocal = amountTotalLocal + amount;
				}
			}
			$scope.valueMap['amountTotalLocal'] = amountTotalLocal;
		}
	}
}]);

app
.controller('btnCtrl', ['$scope', '$http', BtnController]);
function BtnController($scope, $http){
	var gridId = $scope.gridId;
	
	$scope.bindBtn = function(event) {
		switch(event){
		case 'event-to-add': // 新增
		{
			var payType = $scope.valueMap['payType'];
			if(payType == 1) {
            	var listUkey = [];
				//回填数据的代码
				var gridDatas = $scope.subGrid.sonlist;
				for (var i = 0; i < gridDatas.length; i++) {
					listUkey.push(gridDatas[i].relativeId);
				}
				console.log($scope.subGrid.sonlist);
				$scope.showModalCheckBox('THR_TransparencyProgram', 'id', listUkey);
			} else if(payType == 2) {
            	var listUkey = [];
				//回填数据的代码
				var gridDatas = $scope.subGrid.sonlist;
				for (var i = 0; i < gridDatas.length; i++) {
					listUkey.push(gridDatas[i].relativeId);
				}
				$scope.showModalCheckBox('THR_VatApplyDetail', 'id', listUkey);
			} else if(payType == 3) {
				$scope.subGrid.sonlist.push({
					fee: 0
				});
				break;
			} else {
				layer.msg('支付类型有误');
			}
			break;
		}
		case 'event-to-delete': // 删除
		{
			$scope.subGrid.deleteBatch();
			var exchangeRate = parseFloat($scope.valueMap['exchangeRate']); // 计算汇率
			// 计算付款CNY总金额
			if(exchangeRate >= 0) {
				var amountTotalCny = 0;
				for (var i = 0; i < $scope.subGrid.sonlist.length; i++) {
					var amount = parseFloat($scope.subGrid.sonlist[i]['amount']);
					if(amount) {
						amountTotalCny = amountTotalCny + Math.round(amount * exchangeRate * 100) / 100;
					}
				}
				$scope.valueMap['amountTotalCny'] = Math.round(amountTotalCny * 100) / 100;
			}
			
			// 计算付款外币总金额
			{
				var amountTotalLocal = 0;
				for (var i = 0; i < $scope.subGrid.sonlist.length; i++) {
					var amount = parseFloat($scope.subGrid.sonlist[i]['amount']);
					if(amount) {
						amountTotalLocal = amountTotalLocal + amount;
					}
				}
				$scope.valueMap['amountTotalLocal'] = amountTotalLocal;
			}
			break;
		}
	};
	}
}